package cn.tank.controller;

import cn.tank.pojo.Article;
import cn.tank.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: ZLY
 * @Date: 2020/11/7 17:48
 * @Description:
 */
@Controller
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @RequestMapping("/findAll")
    @ResponseBody
    public List<Article> findAll(){
        return articleService.findALl();
    }

    @RequestMapping("/index")
    public String index(Model model,Integer pageNum){
        if(pageNum==null){
            pageNum = 0;
        }

        Sort unsorted = Sort.unsorted();
        unsorted.ascending();

        Pageable pageable = PageRequest.of(pageNum,3,unsorted.ascending());

        Page<Article> page = articleService.findALl(pageable);
        model.addAttribute("pageInfo",page);
        return "client/index";
    }
}
